'use strict';

/**
 * Config for the router
 */
angular.module('app')
	.run(['$rootScope', '$state', '$stateParams', '$location', 'IGNOREPERMISSION',
		function ($rootScope, $state, $stateParams, $location, IGNOREPERMISSION) {
			$rootScope.$state = $state;
			$rootScope.$stateParams = $stateParams;
			
			//加入权限控制
			$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
				if (!fromState.name) return;
				var currentUrl = toState.name;
				var searchVal = currentUrl.replace(/\./g, '');
				if (IGNOREPERMISSION.IGNORELIST.indexOf(searchVal) < 0) {
					// $location.path('/access/login');
					// $state.go('access.login');
				}
			});
		}]
	).config(['$stateProvider', '$urlRouterProvider', '$locationProvider', 'JQ_CONFIG', 'MODULE_CONFIG',
		function ($stateProvider, $urlRouterProvider, $locationProvider, JQ_CONFIG, MODULE_CONFIG) {
			var layout = "/statics/tpl/app.html";
			$urlRouterProvider
				.otherwise('/access/login');
			$stateProvider
				.state('access', {
					url: '/access',
					template: '<div ui-view class="fade-in-right-big smooth"></div>'
				}).state('access.login', {
				url: '/login',
				templateUrl: '/statics/tpl/access/login.html',
				controller: 'LoginCtrl',
				resolve: load(['/statics/js/app/access/login.js'])
			}).state('app', {
				abstract: true,
				url: '/app',
				templateUrl: layout
			}).state('app.instrument', {
				url: '/instrument',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid','/statics/libs/assets/echarts/echarts.min.js','/statics/libs/assets/echarts/china.js'])
			}).state('app.instrument.list', {
				url: '/list',
				templateUrl: '/statics/tpl/instrument/list.html',
				resolve: load(['/statics/js/app/instrument/instrument.js','/statics/js/app/instrument/drawChart.js'])
			}).state('app.application', {
				url: '/application',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid','/statics/libs/assets/echarts/echarts.min.js','/statics/libs/assets/echarts/china.js'])
			}).state('app.application.create', {
				url: '/create',
				templateUrl: '/statics/tpl/application/create/list.html',
				resolve: load(['/statics/js/app/application/create/create.js','/statics/js/app/application/create/applist.js'])
			}).state('app.application.appCreateIn', {
//				url: '/appCreate',
				url: '/appCreate/:id/:taskId/:name/:flag',
//				params: {'id': null,'taskId':null,'name':null,'flag': null},
				templateUrl: '/statics/tpl/application/create/appcreate.html',
				resolve: load(['/statics/js/app/application/create/appCreateIn.js','/statics/js/app/application/create/appCreateLog.js','codeMirror','/statics/js/app/application/create/appCreateAnalyse.js','/statics/js/services/meterService.js'])
			}).state('app.application.develop', {
				url: '/develop',
//				params: {'flag':null},
				templateUrl: '/statics/tpl/application/develop/list.html',
				resolve: load(['/statics/js/app/application/develop/develop.js'])
			}).state('app.application.createDeploy', {
				url: '/createDeploy',
				templateUrl: '/statics/tpl/application/develop/createDeploy.html',
				resolve: load(['/statics/js/app/application/develop/createDeploy.js','codeMirror'])
			}).state('app.application.developDetail', {
//				url: '/developDetail',
				url: '/developDetail/:id/:appName/:appCoreRename/:flag',
//				params: {'id': null,'appName':null,'appCoreRename':null,'flag':null},
				templateUrl: '/statics/tpl/application/develop/developDetail.html',
				resolve: load(['/statics/js/app/application/develop/developDetail.js','codeMirror','/statics/js/app/adhibition/model/tpl_task.js','/statics/js/services/meterService.js'])
			}).state('app.containerManger', {
				url: '/containerManger',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid','/statics/libs/assets/echarts/echarts.min.js','/statics/libs/assets/echarts/china.js'])
			}).state('app.containerManger.container', {
				url: '/container',
				templateUrl: '/statics/tpl/containerManger/container/list.html',
				resolve: load(['/statics/js/app/containerManger/container/container.js','/statics/js/services/meterService.js','codeMirror'])
			}).state('app.containerManger.net', {
				url: '/net',
				templateUrl: '/statics/tpl/containerManger/net/list.html',
				resolve: load(['/statics/js/app/containerManger/net/net.js'])
			}).state('app.containerManger.appMore', {
//				url: '/appMore',
				url: '/appMore/:id',
//				params: {'id': null},
				templateUrl: '/statics/tpl/containerManger/net/appMore.html',
				resolve: load(['/statics/js/app/containerManger/net/appMore.js'])
			}).state('app.containerManger.img', {
//				url: '/img',
				url: '/img/:id/:flag',
//				params: {'id': null,'flag':null},
				templateUrl: '/statics/tpl/containerManger/img/list.html',
				resolve: load(['/statics/js/app/containerManger/img/img.js','/statics/js/app/containerManger/img/shop.js'])
			}).state('app.containerManger.imgDetail', {
//				url: '/imgDetail',
				url: '/imgDetail/:id/:name',
//				params: {'id': null,'name': null},
				templateUrl: '/statics/tpl/containerManger/img/imgDetail.html',
				resolve: load(['/statics/js/app/containerManger/img/imgDetail.js'])
			}).state('app.containerManger.shopDetail', {
//				url: '/shopDetail',
				url: '/shopDetail/:id/:name',
//				params: {'id': null,'name': null},
				templateUrl: '/statics/tpl/containerManger/img/shopDetail.html',
				resolve: load(['/statics/js/app/containerManger/img/shopDetail.js'])
			}).state('app.containerManger.disk', {
				url: '/disk',
				templateUrl: '/statics/tpl/containerManger/disk/list.html',
				resolve: load(['/statics/js/app/containerManger/disk/disk.js'])
			}).state('app.containerManger.containerDetail', {
//				url: '/containerDetail',
				url: '/containerDetail/:conName/:flag',
//				params: {'conName': null,'flag': null},
				templateUrl: '/statics/tpl/containerManger/container/detail.html',
				resolve: load(['/statics/js/app/containerManger/container/detailNew.js','/statics/js/app/factory/eChartLineFactory.js'])
			}).state('app.resources', {
				url: '/resources',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid','/statics/libs/assets/echarts/echarts.min.js','/statics/libs/assets/echarts/china.js'])
			}).state('app.resources.host', {
				url: '/host',
				templateUrl: '/statics/tpl/resources/host/list.html',
				resolve: load(['/statics/js/app/resources/host/host.js'])
			}).state('app.resources.hostDetail', {
				url: '/hostDetail/:id/:hostRealName/:flag',
//				params: {'id': null,"hostRealName":null,'flag':null},
				templateUrl: "/statics/tpl/resources/host/detail.html",
				resolve: load(['/statics/js/app/resources/host/hostDetail.js','/statics/js/app/factory/eChartLineFactory.js'])
			}).state('app.resources.swarm', {
				url: '/swarm',
				templateUrl: '/statics/tpl/resources/swarm/list.html',
				resolve: load(['/statics/js/app/resources/swarm/swarm.js'])
			}).state('app.resources.warehouse', {
				url: '/warehouse',
				templateUrl: '/statics/tpl/resources/warehouse/list.html',
				resolve: load(['/statics/js/app/resources/warehouse/warehouse.js','/statics/js/app/adhibition/model/tpl_task.js'])
			}).state('app.resources.ip', {
				url: '/ip',
				templateUrl: '/statics/tpl/resources/IP/list.html',
				resolve: load(['/statics/js/app/resources/IP/ip.js'])
			}).state('app.userCenter', {
				url: '/userCenter',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid'])
			}).state('app.userCenter.account', {
				url: '/account',
				templateUrl: '/statics/tpl/userCenter/user/list.html',
				resolve: load(['/statics/js/app/userCenter/user/user.js'])
			}).state('app.userCenter.role', {
				url: '/role',
				templateUrl: "/statics/tpl/userCenter/role/list.html",
				resolve: load(['/statics/js/app/userCenter/role/role.js'])
			}).state('app.userCenter.permissions', {
				url: '/permissions',
				templateUrl: "/statics/tpl/userCenter/permissions/list.html",
				resolve: load(['/statics/js/app/userCenter/permissions/permissions.js'])
			}).state('app.environment', {
				url: '/environment',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid'])
			}).state('app.environment.list', {
				url: '/list',
				templateUrl: "/statics/tpl/System/environment/list.html",
				resolve: load(['/statics/js/app/System/environment/environment.js'])
			}).state('app.project', {
				url: '/project',
				template: '<div ui-view class="fade-in-up"></div>',
				resolve: load(['easyui', 'tableGrid'])
			}).state('app.project.list', {
				url: '/list',
				templateUrl: "/statics/tpl/System/project/list.html",
				resolve: load(['/statics/js/app/System/project/project.js'])
			})
			function load(srcs, callback) {
				return {
					deps: ['$ocLazyLoad', '$q','$interval',
						function ($ocLazyLoad, $q,$interval) {
							var deferred = $q.defer();
							var promise = false;
							srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
							if(window.timer){
				                $interval.cancel(window.timer);
				            }
				            delete window.timer;
							if (!promise) {
								promise = deferred.promise;
							}
							angular.forEach(srcs, function (src) {
								//console.log(src);
								promise = promise.then(function () {
									if (JQ_CONFIG[src]) {
										return $ocLazyLoad.load(JQ_CONFIG[src]);
									}
									//if(src.substr(src.length-3, 3) === '.js') {
									//    name = src + '?v=' + OpsConstant.O_VERSION;
									//} else {
									name = src;
									//}
									return $ocLazyLoad.load(name);
								});
							});
							deferred.resolve();
							return callback ? promise.then(function () {
								return callback();
							}) : promise;
						}]
				}
			}
			
			// 去掉angular url的#号
			//$locationProvider.html5Mode(true);
		}
	]
);
